﻿using System;
using System.Numerics;

namespace Mrkz.ImageManipulation
{
    /// <summary>
    /// Represent a pixel in a 3 channel complex image
    /// </summary>
    public class ComplexPixel : IEquatable<ComplexPixel>,IArithmeticOperation<ComplexPixel>,IEquatableWithTolerance<ComplexPixel>,IPixel
    {
        Complex _r;
        Complex _g;
        Complex _b;        

        public ComplexPixel(Complex R, Complex G,Complex B)
        {
            _r = R;
            _g = G;
            _b = B;
        }
        public ComplexPixel(Pixel pixel)
        {
            _r = new Complex(pixel.Red, 0);
            _g = new Complex(pixel.Green, 0);
            _b = new Complex(pixel.Blue, 0);
        }
        public ComplexPixel()
        {
            _r = new Complex(0, 0);            
            _g = new Complex(0, 0);
            _b = new Complex(0, 0);
        }

        public Complex CRed
        {
            get { return _r; }
            set { _r = value; }
        }

        public Complex CGreen
        {
            get { return _g; }
            set { _g = value; }
        }

        public Complex CBlue
        {
            get { return _b; }
            set { _b = value; }
        }
        
        public static ComplexPixel operator +(ComplexPixel A,ComplexPixel B)
        {
            ComplexPixel r = new ComplexPixel(A.CRed + B.CRed, A.CGreen + B.CGreen, A.CBlue + B.CBlue);            
            return r;
        }
        public static ComplexPixel operator -(ComplexPixel A, ComplexPixel B)
        {   
            ComplexPixel r = new ComplexPixel(A.CRed - B.CRed, A.CGreen - B.CGreen, A.CBlue - B.CBlue);
            return r;
        }

        public bool Equals(ComplexPixel other)
        {
            if (CGreen == other.CGreen
                && CBlue == other.CBlue
                && CRed == other.CRed)
                return true;

            return false;
        }

        public ComplexPixel Add(ComplexPixel B)
        {
            return (this + B);
        }

        public ComplexPixel Subtract(ComplexPixel B)
        {
            return (this - B);
        }

        public bool Equals(ComplexPixel other, int tolerance)
        {
            return false;
        }
       
        public double Alpha
        {
            get
            {
                return 1;
            }
            set
            {                
            }
        }

        public double Red
        {
            get
            {
                return CRed.Magnitude;
            }
            set
            {
                CRed = new Complex(value, 0); ;
            }
        }

        public double Green
        {
            get
            {
                return CGreen.Magnitude;
            }
            set
            {
                CGreen = new Complex(value, 0);
            }
        }

        public double Blue
        {
            get
            {
                return CBlue.Magnitude;
            }
            set
            {
                CBlue = new Complex(value, 0);
            }
        }

        public ComplexPixel Multiply(double d)
        {
            return new ComplexPixel(CRed * d, CGreen * d, CBlue * d);
        }

        public ComplexPixel Multiply(double mR, double mG, double mB)
        {
            return new ComplexPixel(CRed * mR, CGreen * mG, CBlue * mB);           
        }
    }
}
